package com.mzy.NetworkProgram.HttpClient;

import com.alibaba.fastjson.JSON;
import com.mzy.common.ApiResult;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author mzy
 * @Date 2022/5/30 13:46
 * @Version 1.0
 */
public class HttpURLConnectionHelper {

    public static String sendRequest(String urlParam,String requestType) {

        HttpURLConnection con = null;

        BufferedReader buffer = null;
        StringBuffer resultBuffer = null;

        try {
            URL url = new URL(urlParam);
            //得到连接对象
            con = (HttpURLConnection) url.openConnection();
            //设置请求类型
            con.setRequestMethod(requestType);
            //设置请求需要返回的数据类型和字符集类型
            con.setRequestProperty("Content-Type", "application/json;charset=GBK");
            //允许写出
            con.setDoOutput(true);
            //允许读入
            con.setDoInput(true);
            //不使用缓存
            con.setUseCaches(false);
            //得到响应码
            int responseCode = con.getResponseCode();

            if(responseCode == HttpURLConnection.HTTP_OK){
                //得到响应流
                InputStream inputStream = con.getInputStream();
                //将响应流转换成字符串
                resultBuffer = new StringBuffer();
                String line;
                buffer = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
                while ((line = buffer.readLine()) != null) {
                    resultBuffer.append(line);
                }
                return resultBuffer.toString();
            }

        }catch(Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    public static void main(String[] args) {

        String url ="http://192.168.0.138:9999/sys/boot/manage/page_find";
        System.out.println(sendRequest(url,"POST"));
        String post = sendRequest(url, "POST");
        // 方式1: 将json字符串转为Java对象
        ApiResult apiResult = JSON.parseObject(post, ApiResult.class);
        System.out.println("java对象-》" + apiResult.getDescribe());
        System.out.println("java对象-》" + apiResult.getCode());
        System.out.println("java对象-》" + apiResult.getTitle());
        System.out.println("java对象-》" + apiResult.getData());
        System.out.println("========================");

    }
}